﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NNote.Properties;

namespace NNote {
	public partial class OptionsDialog : Form {
		Font textFont;
		Color? backColor, textColor;

		public OptionsDialog() {
			InitializeComponent();
		}
		protected override void OnShown(EventArgs e) {
			textFont = Settings.Default.TextFont;
			backColor = Settings.Default.BackColor;
			textColor = Settings.Default.TextColor;
			BindSettings();
			base.OnShown(e);
		}

		private void BindSettings() {
			var _textFont = textFont ?? SystemFonts.DefaultFont;
			var _textColor = textColor ?? SystemColors.WindowText;
			var _backColor = backColor ?? SystemColors.Window;
			fontLink.Text = string.Format(
				"{0}, {1}, {2}",
				_textFont.FontFamily.Name,
				_textFont.SizeInPoints,
				_textFont.Style
			);
			backColorButton.BackColor = _backColor;
			textColorButton.BackColor = _textColor;
			textBox1.Font = _textFont;
			textBox1.BackColor = _backColor;
			textBox1.ForeColor = _textColor;
		}

		private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
			fontDialog1.Font = textFont ?? SystemFonts.DefaultFont;
			var result = fontDialog1.ShowDialog(this);
			switch (result) {
				case DialogResult.OK:
					textFont = fontDialog1.Font;
					BindSettings();
					break;
				default: throw new NotImplementedException();
			}
		}

		private void bgColorButton_Click(object sender, EventArgs e) {
			colorDialog1.Color = backColor ?? SystemColors.Window;
			var result = colorDialog1.ShowDialog(this);
			switch (result) {
				case DialogResult.OK:
					backColor = colorDialog1.Color;
					BindSettings();
					break;
				default: throw new NotImplementedException();
			}
		}

		private void fgColorButton_Click(object sender, EventArgs e) {
			colorDialog1.Color = textColor ?? SystemColors.WindowText;
			var result = colorDialog1.ShowDialog(this);
			switch (result) {
				case DialogResult.OK:
					textColor = colorDialog1.Color;
					BindSettings();
					break;
				default: throw new NotImplementedException();
			}
		}

		private void okButton_Click(object sender, EventArgs e) {
			Settings.Default.TextColor = textColor;
			Settings.Default.BackColor = backColor;
			Settings.Default.TextFont = textFont;
			Settings.Default.Save();
		}
	}
}
